<template>
    <div class="img-list-box" v-loading="loading">
        <el-input
            v-model.trim="keyword"
            placeholder="请输入查询关键字"
            clearable
            @keyup.enter.native="getImgList"
            style="width: 180px;margin-left:5px;">
        </el-input>

        <div class="img-box" v-if="imgList.length">
            <div class="img-item" v-for="(item, index) in imgList" :key="index">
                <div class="show-box">
                    <img :src="item.url" alt="" class="img">
                </div>
                <div class="img-btn">
                    <el-button type="success" size="mini" @click="check(item)">选中</el-button>
                </div>
            </div>
        </div>

        <div class="no-img-list" v-else>没找着图片呢，只能手动上传喽~</div>

        <div class="tools">
            <el-pagination
                @size-change="sizeChange"
                @current-change="pageChange"
                :current-page="pageNum"
                :page-sizes="_pageSizes"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
        </div>
    </div>
</template>

<script>
export default {
    data () {
        return {
            loading: false,
            list: [],
            pageNum: 1,
            pageSize: 10,
            total: 0,
            keyword: ''
        }
    },
    props: {
        gameName: {}
    },
    created () {
        this.keyword = this.gameName || ''
        this.getImgList()
    },
    watch: {
    },
    methods: {
        // 获取视频列表
        async getImgList () {
            if (this.loading) return
            this.pageNum = 1
            this.loading = true
            let params = {
                keyword: this.keyword
            }
            let res = await this.$http.post('/home/findGameSource', params)
            if (res.code === '0') {
                this.list = res.data || []
                this.total = this.list.length || 0
            }
            this.loading = false
        },

        // 选中
        check (item) {
            this.$emit('check', item)
        },

        sizeChange (val) {
            this.pageNum = 1
            this.pageSize = val
        },
        pageChange (val) {
            this.pageNum = val
        }
    },
    computed: {
        imgList () {
            let start = this.pageNum * this.pageSize - this.pageSize
            let end = this.pageNum * this.pageSize
            return this.list.slice(start, end)
        }
    }
}
</script>

<style lang="scss" scoped>
    .img-list-box {
        .img-box {
            min-height: 324px;
            display: flex;
            flex-wrap: wrap;
            margin-top: 10px;
            .img-item {
                padding: 5px;
                padding-top: 10px;
                .show-box {
                    width: 160px;
                    height: 160px;
                    border-radius: 6px;
                    overflow: hidden;
                    display: flex;
                    align-items: center;
                    > img {
                        width: 100%;
                        max-height: 100%;
                    }
                }
                &:hover {
                    background-color: #eeeeee;
                }
                .img-btn {
                    margin-top: 10px;
                    display: flex;
                    // align-items: center;
                    justify-content: center;
                }
            }
        }
        .no-img-list {
            display: flex;
            align-items: center;
            justify-content: center;
            min-height: 200px;
        }
        .tools {
            padding-right: 20px;
            display: flex;
            justify-content: flex-end;
        }
    }
</style>
